//摇杆逻辑
class RockerLogic {
	public constructor() {
	}
	private static instance: RockerLogic;
	public static getInstance(): RockerLogic {
		if (this.instance == null) {
			this.instance = new RockerLogic();
		}
		return this.instance;
	}
	/**此ui为摇杆 */
	public ui: Rocker;
	private player: Player;
	private ballRadius: number = 0;      //小球半径
	private circleRadius: number = 0;    //圆环半径
	private centerX: number = 0;         //中心点坐标
	private centerY: number = 0;
	private touchID: number;             //触摸ID
	/**攻击按钮是否隐藏*/
	public isatk: boolean;


	public openUI(con: any, _ui: Rocker, arg: any = null): void {
		if (this.ui == null) {
			this.ui = _ui;
			con.addChild(this.ui);
			if (this.ui.inited) {
				this.init();
			}
			this.ui.logic = this;
		}
		/**显示或隐藏摇杆 */
		//this.ui.visible = false;
	}

	public init() {
		//console.log("RockerLogic.init", this.ui.inited, this.ui.logic);
		/**中间球半径 */
		this.ballRadius = this.ui.ball.height / 2;
		/**外面球半径 */
		this.circleRadius = this.ui.circle.height / 2;
		//获取中心点
		this.centerX = this.circleRadius;

		this.centerY = this.circleRadius;

		//设置锚点
		this.ui.anchorOffsetX = this.circleRadius;

		this.ui.anchorOffsetY = this.circleRadius;

		this.ui.ball.anchorOffsetX = this.ballRadius;

		this.ui.ball.anchorOffsetY = this.ballRadius;

		//设置小球初始位置
		this.ui.ball.x = this.centerX;
		console.log(this.ui.ball.x);
		this.ui.ball.y = this.centerY;
		console.log(this.ui.ball.y);
		//this.initCrood();
		//this.stopbtn();
	}
    // public initCrood() {
	// 	//摇杆在屏幕初始位置
	// 	this.ui.x = GameData.GameStage.stageWidth / 5 - 60;
	// 	this.ui.y = GameData.GameStage.stageHeight - this.circleRadius - 20;
	// }
	/**摇滚是否启动的布尔值，初始为真 */
	private started: boolean;
	/**启动虚拟摇杆 (监听事件根据实际情况设置，不然点一下UI上的其他按钮，也会触发虚拟摇杆事件。)*/
	public start() {
		this.started = true;
		if (!GameData.GameStage.hasEventListener(egret.TouchEvent.TOUCH_BEGIN)) {
			GameData.GameStage.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onTouchBegin, this);
		}
		//if (!GameData.GameStage.hasEventListener(egret.TouchEvent.TOUCH_END)) {
			GameData.GameStage.addEventListener(egret.TouchEvent.TOUCH_END, this.onTouchEnd, this);
		//}
		if (!GameData.GameStage.hasEventListener(egret.TouchEvent.TOUCH_MOVE)) {
			GameData.GameStage.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.onTouchMove, this);
		}
	}
	/**停止虚拟摇杆*/
	public stop() {
		this.started = false;
		GameData.GameStage.removeEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onTouchBegin, this);
		GameData.GameStage.removeEventListener(egret.TouchEvent.TOUCH_END, this.onTouchEnd, this);
		GameData.GameStage.removeEventListener(egret.TouchEvent.TOUCH_MOVE, this.onTouchMove, this);
		this.touchID = null;
	}
	private p3: egret.Point = new egret.Point();
	/**在哪触摸开始，就在哪显示虚拟摇杆*/
	private onTouchBegin(e: egret.TouchEvent) {
		if (Maplogic.getInstance().role != null && Maplogic.getInstance().role.state == PLAYERSTATE.DEAD) {
			return;
		}
		
		//this.player = Player.getInstance();
		//点击摇杆时 屏蔽所有碰撞
		GameData.GameStage.$hitTest = (x, y) => this.ui;
		this.ui.circle.$hitTest = (x, y) => this.ui;
		this.touchID = e.touchPointID;
		this.p3.x = e.stageX;
		this.p3.y = e.stageY;
		if(this.p3.x<500){
		this.ui.visible = true;
				this.ui.x = this.p3.x
		        this.ui.y = this.p3.y
		        this.p1.x = this.ui.x;
		        this.p1.y = this.ui.y;
		        this.ui.ball.x = this.centerX;
		        this.ui.ball.y = this.centerY;
				}
	
		if (this.p3.x > 950 && this.p3.x < 1100 && this.p3.y > 430 && this.p3.y < 580) {
			this.isatk = false;
			//this.player.state==PLAYERSTATE.ATK;
			//Maplogic.getInstance().role.state == PLAYERSTATE.ATK;
		}
		else {
			this.isatk = true;
		}
	}
	//触摸移动，设置小球的位置
	private p1: egret.Point = new egret.Point();
	private p2: egret.Point = new egret.Point();
	private changetime: number;
	public btn1() {
		if (this.isatk == false) {
			this.isatk = true;
		}
		else {
			return;
		}
	}
	private onTouchMove(e: egret.TouchEvent) {
		if (Maplogic.getInstance().role != null && Maplogic.getInstance().role.state == PLAYERSTATE.DEAD) {
			return;
		}
		this.p2.x = e.stageX;
		this.p2.y = e.stageY;
		//计算两点距离
		let dist = egret.Point.distance(this.p1, this.p2);
		var angle: number = Math.atan2(e.stageY - this.ui.y, e.stageX - this.ui.x);

		//console.log(angle+"滑动方向")
		//手指距离在圆环范围内		
	        if (this.p2.x <500) {			
			if (dist <= (this.circleRadius - this.ballRadius)) {
				this.ui.ball.x = this.centerX + e.stageX - this.ui.x;
				this.ui.ball.y = this.centerY + e.stageY - this.ui.y;
				//手指距离在圆环范围外
			} else {
				this.ui.ball.x = Math.cos(angle) * (this.circleRadius - this.ballRadius) + this.centerX;
				this.ui.ball.y = Math.sin(angle) * (this.circleRadius - this.ballRadius) + this.centerY;
			}
			if (dist >= 3) {
				let p1 = new egret.Point(this.ui.ball.x, this.ui.ball.y);
				let p2 = new egret.Point(this.centerX, this.centerY);
				let dis = egret.Point.distance(p1, p2);
				let rate = dis / (this.circleRadius - this.ballRadius);
				this.moveBall(angle, rate);
			}
			this.changetime = egret.getTimer();
			e.stopImmediatePropagation();
		}
	}

	//触摸结束，隐藏虚拟摇杆
	/**P4是专门用来检测左侧摇杆的点 */
	private p4: egret.Point = new egret.Point();
	public onTouchEnd(e: egret.TouchEvent) {
		this.p4.x=e.stageX;
		//触摸结束时隐藏摇杆
		if(this.p4.x<500)
		{
		this.ui.visible = false;
		}
		GameLogic.getInstance().openHit();
		this.touchID = null;
		if (Maplogic.getInstance().role != null && Maplogic.getInstance().role.state == PLAYERSTATE.DEAD) {
			return;
		}
		this.p2.x = e.stageX;
		this.p2.y = e.stageY;
		if (this.controlplayer != null) {
			this.controlplayer.updateDirection(null);
		}
		//this.initCrood();
		//小球恢复初始位置
		// this.ui.ball.x = this.centerX;
		// this.ui.ball.y = this.centerY;

		if (this.p3.x > 950 && this.p3.x < 1100 && this.p3.y > 430 && this.p3.y < 580) {
			this.isatk = true;
		}
	}

	private controlplayer: RolePlayer;
	/**要控制的球 */
	public setControlPlayer(ball: RolePlayer) {
		this.controlplayer = ball;
	}
	/**球体移动 */
	private moveBall(hu, rate) {
		if (this.controlplayer != null) {
			let dir = hu * 180 / Math.PI + 90;
			this.controlplayer.updateDirection(dir);
		}
	}
	public clear() {
		this.controlplayer = null;
		this.stop();
		if (this.ui != null && this.ui.parent != null) {
			this.ui.parent.removeChild(this.ui);
		}
		this.ui = null;
	}
}